package com.raymond.smpp.codec.encodeing;

import com.raymond.client.codec.encodeing.BaseEncoder;
import com.raymond.client.constants.GlobalConstants;
import com.raymond.client.msg.BaseMessage;
import com.raymond.client.utils.NettyByteBufUtil;
import com.raymond.cmpp.utils.CommonUtil;
import com.raymond.smgp.msg.request.SubmitRequestMessage;
import com.raymond.smgp.msg.tlv.TLV;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.util.List;

/**
 * 短信提交请求编码
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-08-05 18:01
 */
public class SubmitRequestMessageEncoder extends BaseEncoder {
    public static final short DEFAULT_VERSION = 0x30;
    @Override
    public byte[] encode0(BaseMessage msg) throws Exception {
        SubmitRequestMessage requestMessage = (SubmitRequestMessage) msg;
        ByteBuf bodyBuffer = Unpooled.buffer();
        bodyBuffer.writeByte(requestMessage.getMsgType());
        bodyBuffer.writeByte(requestMessage.getNeedReport());
        bodyBuffer.writeByte(requestMessage.getPriority());
        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getServiceId().getBytes
                (GlobalConstants.DEFAULT_TRANSPORT_CHARSET), 10));
        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getFeeType().getBytes
                (GlobalConstants.DEFAULT_TRANSPORT_CHARSET), 2));
        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getFeeCode().getBytes
                (GlobalConstants.DEFAULT_TRANSPORT_CHARSET), 6));
        if (0x13 != DEFAULT_VERSION) {
            bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getFixedFee().getBytes
                    (GlobalConstants.DEFAULT_TRANSPORT_CHARSET), 6));
        }
        bodyBuffer.writeByte(requestMessage.getMsgFmt());
        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getValidTime().getBytes
                (GlobalConstants.DEFAULT_TRANSPORT_CHARSET), 17));
        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getAtTime().getBytes
                (GlobalConstants.DEFAULT_TRANSPORT_CHARSET), 17));
        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getSrcTermId().getBytes
                (GlobalConstants.DEFAULT_TRANSPORT_CHARSET), 21));
        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getChargeTermId().getBytes
                (GlobalConstants.DEFAULT_TRANSPORT_CHARSET), 21));
        bodyBuffer.writeByte(requestMessage.getDestTermIdCount());

        for (int i = 0; i < requestMessage.getDestTermIdCount(); i++) {
            String[] destTermId = requestMessage.getDestTermIdArray();
            bodyBuffer.writeBytes(CommonUtil.ensureLength(destTermId[i].
                    getBytes(GlobalConstants.DEFAULT_TRANSPORT_CHARSET), 21));
        }
        byte[] msgContentBytes = CommonUtil.getMsgContentBytes(requestMessage.getMsgFmt(), requestMessage.getMsgContent());
        bodyBuffer.writeByte(msgContentBytes.length);

        bodyBuffer.writeBytes(msgContentBytes);
        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getReserve().getBytes
                (GlobalConstants.DEFAULT_TRANSPORT_CHARSET), 8));

        List<TLV> tabs = requestMessage.getTlvList();
        for (TLV tlv : tabs) {
            bodyBuffer.writeShort(tlv.getTag());
            bodyBuffer.writeShort(tlv.getLength());
            bodyBuffer.writeBytes(tlv.getValueData());
        }
        return NettyByteBufUtil.toArray(bodyBuffer, bodyBuffer.readableBytes());
    }
}
